package com.mdd.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.entity.Shop;
import com.mdd.common.entity.user.User;
import com.mdd.common.entity.user.UserGoods;
import com.mdd.common.mapper.goods.GoodsMapper;
import com.mdd.common.mapper.user.UserGoodsMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.front.service.IUserGoodsService;
import com.mdd.front.vo.shop.ShopProductListedVo;
import com.mdd.front.vo.user.UserGoodsListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class UserGoodsServiceImpl implements IUserGoodsService {
    @Resource
    UserGoodsMapper userGoodsMapper;

    @Resource
    GoodsMapper goodsMapper;

    /**
     * 关注
     * @param userGoods
     * @return
     */
    @Override
    public AjaxResult<Object> focusGoods(UserGoods userGoods) {
        System.out.println("getUserId:"+ userGoods.getUserId() + "--getGoodsId:" + userGoods.getGoodsId());
        UserGoods userg = userGoodsMapper.selectOne(new LambdaQueryWrapper<UserGoods>().eq(UserGoods::getUserId, userGoods.getUserId()).eq(UserGoods::getGoodsId, userGoods.getGoodsId()));
        if(userg == null) {
            userGoodsMapper.insert(userGoods);
        }else{
            Assert.isNull(userGoods, "请勿重复关注");
        }
        return AjaxResult.success();
    }

    /**
     * 取消关注
     * @param
     * @return
     */
    @Override
    public AjaxResult<Object> cancellationFocus(Integer userId,Integer goodsId) {
        UserGoods userGoods = userGoodsMapper.selectOne(new LambdaQueryWrapper<UserGoods>().eq(UserGoods::getUserId, userId).eq(UserGoods::getGoodsId, goodsId));
        //Assert.isNull(userGoods, "已取消");
        System.out.println("getUserId:"+ userGoods.getUserId() + "--getGoodsId:" + userGoods.getGoodsId());
        if(userGoods != null){
            userGoodsMapper.deleteById(userGoods.getId());
        }
        return AjaxResult.success();
    }

    /**
     * 集合
     * @param userId 用户id
     * @return
     */
    @Override
    public List<UserGoodsListedVo> list(Integer userId) {
        MPJQueryWrapper<UserGoods> mpjQueryWrapper = new MPJQueryWrapper<>();
        mpjQueryWrapper.eq("user_id", userId);
        List<UserGoods> list = userGoodsMapper.selectList(mpjQueryWrapper);
        return __copyList(list);
    }

    private List<UserGoodsListedVo> __copyList(List<UserGoods> userGoodsList) {
        List<UserGoodsListedVo> list = new ArrayList<>();
        for (UserGoods userGoods : userGoodsList) {
            UserGoodsListedVo userGoodsListedVo = new UserGoodsListedVo();
            BeanUtils.copyProperties(userGoods, userGoodsListedVo);
            userGoodsListedVo.setGoods(goodsMapper.selectById(userGoods.getGoodsId()));
            list.add(userGoodsListedVo);
        }
        return list;
    }


    /**
     * 是否关注
     * @param userId
     * @param goodsId
     * @return
     */
    @Override
    public boolean isFocus(Integer userId,Integer goodsId) {
        System.out.println("sfuserId:"+ userId + "--goodsId:" + goodsId);
        UserGoods userGoods = userGoodsMapper.selectOne(new LambdaQueryWrapper<UserGoods>().eq(UserGoods::getUserId, userId).eq(UserGoods::getGoodsId, goodsId));
        if(userGoods == null){
            return false;
        }else{
            return true;
        }
    }
}
